//
//  main.m
//  HomeWork 06 调研NSDate、预习内存管理
//
//  Created by dllo on 16/7/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
//    NSDate
#if 0
//     1.计算当前时间和一个固定时间的差值,如果差值在60秒内,输出"刚刚",如果在60秒外3600秒内,输出"xx分钟前",如果3600秒外, 3600*24秒内,输出"xx小时前"
    //计算当前时间
    NSDate *today = [NSDate date];
    NSDateFormatter *dataFormater = [[NSDateFormatter alloc]init];
    [dataFormater setDateFormat:@"HH:mm:ss"];
   // NSString *formatterDateString = [dataFormater stringFromDate:today];
    NSString *time = @"15:30:20";
    NSDate *d = [dataFormater dateFromString:time];
    NSTimeInterval late = [d timeIntervalSince1970] * 1;
    
  //  NSDate *dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now = [today timeIntervalSince1970] * 1;
  
    NSTimeInterval cha = now - late;
    NSLog(@"3333:%@",[NSString stringWithFormat:@"%f",cha]);
    if (cha/3600 < 1) {
        NSLog(@"gangang");
    } else if (cha/3600 > 1 && cha / 86400 < 1) {
        NSLog(@"4444:%@",[NSString stringWithFormat:@"%f分钟前",cha/3600]);
    }else if (cha /86400  > 1 && cha/ 86400 < 24){
        NSLog(@"555:%@",[NSString stringWithFormat:@"%f",cha / 86400]);
    }
    
    
     
     
     
//    2. 将字符串@"2016年04月01日 01点20分18秒"转换为NSDate对象。
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];
    NSString *dateStr = @"2016年04月01日 01点20分18秒";
    NSDate *date1 = [dateFormat dateFromString:dateStr];
    //NSLog(@"%@",[dateFormat stringFromDate:date1]);
    NSLog(@"Date1:%@",date1);
//     3.将1468232420转成日期类型
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:1468232420];
    
    NSDateFormatter *dateformatter1 = [[NSDateFormatter alloc]init];
    [dateformatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *formatterdateStr = [dateformatter1 stringFromDate:date];
    NSLog(@"666:%@",formatterdateStr);
    
    
#endif
    
//    4. 研究NSDateComponents
//     预习内存管理, 整理今天的代码
#if 0
    NSDateComponents *comps = [[NSDateComponents alloc]init];
    [comps setDay:6];
    [comps setMonth:5];
    [comps setYear:2016];
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
   NSDate *date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
    NSInteger weekday = [comps weekday];
    NSLog(@"%ld",weekday);
    
    
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
  NSLog(@"%@",currentCalendar);
#endif
    //calculate today’s day and weekday.
    
    NSDate *today = [NSDate date];
    NSCalendar *gregprian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayCompants = [ gregprian components:NSDayCalendarUnit fromDate:today];
    NSInteger day = [weekdayCompants day];
    NSInteger weekday = [weekdayCompants weekday];
    NSLog(@"%ld  %ld",day,weekday);
    
    
    return 0;
}
